﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data.Common;
using System.Data;

namespace Jacaranda.Data
{
    class OleDbDataAccess : DataAccess
    {
        public override DbConnection CreateDbConnection()
        {
            return new OleDbConnection();
        }

        public override DbCommand CreateDbCommand()
        {
            return new OleDbCommand();
        }

        public override DbDataAdapter CreateDbDataAdapter()
        {
            return new OleDbDataAdapter();
        }

        public override DbCommandBuilder CreateDbCommandBuilder()
        {
            return new OleDbCommandBuilder();
        }

        public override DbParameter CreateDbParameter()
        {
            return new OleDbParameter();
        }

        public override string AssociationTableTimestampColumnName
        {
            get { return "CreateTime"; }
        }

        public override string CreateParameterizedExpression(string parameterName)
        {
            return string.Format("@{0}", parameterName);
        }

        public override void DeleteTableData(string tableName)
        {
            OleDbConnection conn = DataPortal.CreateConnection() as OleDbConnection;

            conn.Open();
            DataTable table = conn.GetSchema("tables");
            conn.Close();

            if (table.AsEnumerable().FirstOrDefault(i => i.Field<string>("TABLE_NAME") == tableName) != null)
            {
                DataPortal.Execute(string.Format("DELETE FROM {0}", tableName));
            }
        }

        public override void DropTable(string tableName)
        {
            OleDbConnection conn = DataPortal.CreateConnection() as OleDbConnection;

            conn.Open();
            DataTable table = conn.GetSchema("tables");
            conn.Close();

            if (table.AsEnumerable().FirstOrDefault(i => i.Field<string>("TABLE_NAME") == tableName) != null)
            {
                DataPortal.Execute(string.Format("DROP TABLE {0}", tableName));
            }
        }

        public override void DropFunction(string functionName)
        {
        }

        public override string CreatePagingSelectStatement(Model.ConditionalExpression expression, Type entityType, string fields, Model.SortCriteria sort, Model.SearchPaginationCriteria page)
        {
            throw new NotImplementedException();
        }
    }
}
